package com.yty.newconsult.service.impl;

import com.yty.newconsult.entity.News;
import com.yty.newconsult.entity.NewsLabel;
import com.yty.newconsult.dao.NewsLabelDao;
import com.yty.newconsult.service.NewsLabelService;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (NewsLabel)表服务实现类
 *
 * @author makejava
 * @since 2025-04-07 16:47:14
 */
@Service("newsLabelService")
public class NewsLabelServiceImpl implements NewsLabelService {
    @Resource
    private NewsLabelDao newsLabelDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public NewsLabel queryById(Integer id) {
        return this.newsLabelDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param newsLabel 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<NewsLabel> queryByPage(NewsLabel newsLabel, PageRequest pageRequest) {
        long total = this.newsLabelDao.count(newsLabel);
        return new PageImpl<>(this.newsLabelDao.queryAllByLimit(newsLabel, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param newsLabel 实例对象
     * @return 实例对象
     */
    @Override
    public NewsLabel insert(NewsLabel newsLabel) {
        this.newsLabelDao.insert(newsLabel);
        return newsLabel;
    }

    @Override
    public Page<NewsLabel> getNewsPage(int page, int limit, String label) {
        Pageable pageable = PageRequest.of(page, limit);
        List<NewsLabel> list =new ArrayList<>();
        if(label.equals("全部")){
            list = newsLabelDao.queryAllByPage(pageable);
        }else{
            list = newsLabelDao.queryPageByLabel(pageable,label);
        }
        long total = newsLabelDao.countAll();
        return new PageImpl<>(list, pageable, total);
    }

    /**
     * 修改数据
     *
     * @param newsLabel 实例对象
     * @return 实例对象
     */
    @Override
    public NewsLabel update(NewsLabel newsLabel) {
        this.newsLabelDao.update(newsLabel);
        return this.queryById(newsLabel.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.newsLabelDao.deleteById(id) > 0;
    }
}
